﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ScheduleManagement.DAL
{
    /// <summary>
    /// 数据访问层_登陆处理类
    /// </summary>
    public class Login_Dal
    {
        //读取配置文件,配置数据库连接字符串
        string cn = Properties.Settings.Default.ConnectionString;

        /// <summary>
        /// 列出所有的用户信息并根据最后登陆时间排序
        /// </summary>
        /// <returns></returns>
        public SqlDataReader List()
        {
            string commandText = "p_select";
            SqlDataReader dr = SqlHelper.ExecuteReader(cn, CommandType.StoredProcedure, commandText);
            return dr;
        }

        /// <summary>
        /// 检查用户是否为记住密码,如果为真，则返回密码，否则返回""
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        
        public string IsRemember(string username)
        {
            string commandText = "p_isremember";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0]=new SqlParameter("@u_Name",username);
            commandParameters[1]=new SqlParameter("@u_Password",SqlDbType.VarChar,30);
            commandParameters[1].Direction=ParameterDirection.Output;
            int i = Convert.ToInt32(SqlHelper.ExecuteScalar(cn, CommandType.StoredProcedure,commandText,commandParameters));//返回值为1则
            if (i == 1)
            {
                return commandParameters[1].Value.ToString();
            }
            else
            {
                return "";
            }
        }



        /// <summary>
        /// 查询用户是否合法
        /// </summary>
        /// <param name="userinfo">用户表结构</param>
        /// <returns></returns>

        public int CheckUser()
        {
            string commandText = "p_checkuser";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@u_Name", ScheduleManagement.MODEL.Users.U_Name);
            commandParameters[1] = new SqlParameter("@u_Password", ScheduleManagement.MODEL.Users.U_Password);
            commandParameters[2] = new SqlParameter("@ch_Id", SqlDbType.Int, 1);
            commandParameters[2].Direction = ParameterDirection.Output;
            int flag =Convert.ToInt32(SqlHelper.ExecuteScalar(cn, CommandType.StoredProcedure, commandText, commandParameters));
            return flag;
        }

        /// <summary>
        /// 更新用户登陆信息表
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="chkstate">记住密码状态</param>
        public void UpdateLoginInfo(string username,int chkstate)
        {
            try
            {
                string commandText = "p_updatelogininfo";
                SqlParameter[] commandParameters = new SqlParameter[2];
                commandParameters[0] = new SqlParameter("@u_Name",username);
                commandParameters[1] = new SqlParameter("@l_Rememberpsw", chkstate);
                SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, commandText, commandParameters);
            }
            catch (Exception) {}//遇到错误跳过            
        }

        public int ReturnUserId()
        {
            string commandText = "p_returnUserId";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@u_Name",ScheduleManagement.MODEL.Users.U_Name);
            return (int)SqlHelper.ExecuteScalar(cn, CommandType.StoredProcedure, commandText, commandParameters);
        }
    }
}
